home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / comm / thor / GMAutoWrite.lha / GMAutoWrite.thor < prev   
Encoding:
Text File  |  1999-10-23  |  8.4 KB  |  323 lines

  1. /*
  2. ** $VER: GMAutoWrite.thor 4.002 (23.10.99)
  3. **       © Gian Maria Calzolari <gcalzo@geocities.com>
  4. **
  5. **       Thanks to Neil Bothwick to let me use his AutoReply as a start base
  6. **       for the adaption of my script originally written for EMS (FidoNet
  7. **       mailer)
  8. **
  9. **  FUNCTION:
  10. **      Automatically write a message at desired intervals, can be called
  11. **      anytime but's best to call it at Thor startup
  12. **
  13. ** $HISTORY:
  14. **
  15. ** 23 Oct 1999 : 004.002 : Added "First-Of-Month" support for Kimme Utsi and,
  16. **                         since it was easy, also a "fixed-day-of-the-month" too! :-)
  17. ** 17 Oct 1999 : 004.001 : added options ExtPgm & ExtPgmType (Simone "Wiz" Tellini)
  18. ** 16 Oct 1999 : 004.000 : First public version!! :-)
  19. ** 15 Oct 1999 : 003.007 : First internal beta! OptionalTags are ignored... :-(
  20. ** 11 Oct 1999 : 003.006 : Started adaption to Thor
  21. **
  22. ** v3.0 11-10-93 Version for EMS 1.0
  23. ** v2.0 28-06-93 Version for GCC 4.0
  24. **
  25. */
  26.  
  27. VerStr     = subword(sourceline(2),3)
  28. ConfigFile = 'ENV:Thor/GMAutoWrite.cfg'
  29.  
  30. true  = 1
  31. false = 0
  32.  
  33. /* This tags can be omitted, default will be "blank" */
  34. TagOptsBlk = "ToName Subject SigFile HdrFile FtrFile ExtPgm"
  35.  
  36. /* This tags can be omitted, default will be "zero" */
  37. TagOptsZro = "ExtPgmType Prog_PVT Date_PVT"
  38.  
  39. TagOptions = "System Conf Active Days BdyFile ToAddr" TagOptsBlk TagOptsZro
  40. NumOpts = words(TagOptions)
  41.  
  42. /* Load bbsread.library if necessary */
  43. if ~show('p', 'BBSREAD') then do
  44.     address command
  45.     'run >nil: `GetEnv THOR/THORPath`bin/LoadBBSRead'
  46.     'WaitForPort BBSREAD'
  47. end
  48.  
  49. call ReadConfig
  50.  
  51. if Tags.0 > 0 then do
  52.    progr = date('I')
  53.    today = Dom()
  54.  
  55.    do i = 1 to Tags.0
  56.       call Validate
  57.  
  58.       if (Tags.i.ACTIVE) then do
  59.          OkToGo = false
  60.  
  61.          if Tags.i.DAYS < 0 then do
  62.             if today = abs(Tags.i.DAYS) then
  63.                OkToGo = true
  64.            end
  65.            else
  66.             if progr - Tags.i.PROG_PVT >= Tags.i.DAYS then
  67.                OkToGo = true
  68.  
  69.          if OkToGo then do
  70.             if Tags.i.EXTPGM ~= '' then ExecutePgm(Tags.i.EXTPGM, Tags.i.EXTPGMTYPE, Tags.i.BDYFILE)
  71.  
  72.             call WriteMsg
  73.             Tags.i.PROG_PVT = progr
  74.             Tags.i.DATE_PVT = date('E')
  75.          end
  76.       end
  77.    end
  78.  
  79.    call WriteConfig
  80. end
  81.  
  82. exit
  83.  
  84. /* ...game over... */
  85.  
  86.  
  87. Validate:
  88.  
  89.    do y = 1 to NumOpts
  90.       Opt = upper(word(TagOptions, y))
  91.       OptDef = symbol('Tags.i.Opt')
  92.  
  93.       Select
  94.           When find(upper(TagOptsBlk),Opt) > 0 then
  95.             if OptDef ~= 'VAR' then Tags.i.Opt = ''
  96.           When find(upper(TagOptsZro),Opt) > 0 then
  97.             if OptDef ~= 'VAR' then Tags.i.Opt = 0
  98.           Otherwise
  99.             if OptDef ~= 'VAR' then call ExitMsg("'" || Opt || "' not defined in tag '" || Tags.i || "'")
  100.       end
  101.  
  102.       if Opt = 'EXTPGM' & Tags.i.Opt ~='' & pos('%F',upper(Tags.i.Opt)) = 0 then
  103.          call ExitMsg("%F in '" || Opt || "' missing in tag '" || Tags.i || "'")
  104.  
  105.    end
  106. return
  107.  
  108.  
  109. ExecutePgm:
  110.    /*
  111.    ** This will process an external pgm or arexx script to create the
  112.    ** message body
  113.    */
  114.    InsertPos = pos('%F',upper( arg(1) ))
  115.    Pgm = left(arg(1),InsertPos-1) || arg(3) || substr(arg(1),InsertPos+2)
  116.  
  117.    if Arg(2) = 0 then         /* Arexx */
  118.       Pgm
  119.      else                     /* dos   */
  120.       address command Pgm
  121.  
  122. return RESULT
  123.  
  124. WriteMsg:
  125.     address BBSREAD
  126.     drop MsgBody. MsgHead.
  127.  
  128.     /* Create message file */
  129.     UNIQUEMSGFILE bbsname '"'Tags.i.System'"' stem MsgFile
  130.  
  131.     if ~open(out,MsgFile.NAME,'w') then call ExitMsg('Unable to create message file')
  132.  
  133.     /* Write headers */
  134.     headline = 'X-Generator:' VerStr
  135.     call writeln(out,headline)
  136.  
  137.     if Tags.i.HDRFILE ~= '' then do
  138.  
  139.         if ~open(headers,Tags.i.HDRFILE,'R') then call ExitMsg('Unable to open header file' Tags.i.HDRFILE)
  140.  
  141.         do until eof(headers)
  142.             call writeln(out,readln(headers))
  143.         end
  144.  
  145.         call close(headers)
  146.     end
  147.  
  148.     call writeln(out,'')
  149.  
  150.     /* Write message body */
  151.  
  152.     if ~open(body,Tags.i.BDYFILE,'R') then call ExitMsg('Unable to open body file' Tags.i.BDYFILE)
  153.  
  154.     do until eof(body)
  155.         call writeln(out,readln(body))
  156.     end
  157.  
  158.     call close(body)
  159.  
  160.     /* Add signature */
  161.     if Tags.i.SIGFILE ~= '' then do
  162.  
  163.         if ~open(sig,Tags.i.SIGFILE,'R') then call ExitMsg('Unable to open signature file' Tags.i.SIGFILE)
  164.  
  165.         do until eof(sig)
  166.             call writeln(out,readln(sig))
  167.         end
  168.  
  169.         call close(sig)
  170.     end
  171.  
  172.     /* Add footer file */
  173.     if Tags.i.FTRFILE ~= '' then do
  174.  
  175.         if ~open(foot,Tags.i.FTRFILE,'R') then call ExitMsg('Unable to open footer file' Tags.i.FTRFILE)
  176.  
  177.         do until eof(foot)
  178.             call writeln(out,readln(foot))
  179.         end
  180.  
  181.         call close(foot)
  182.     end
  183.  
  184.     call close(out)
  185.  
  186.     /* Create EMail event */
  187.     drop EventData.
  188.     EventData.TONAME     = Tags.i.TONAME
  189.     EventData.TOADDR     = Tags.i.TOADDR
  190.     EventData.SUBJECT    = Tags.i.SUBJECT
  191.     EventData.CONFERENCE = Tags.i.CONF
  192.     EventData.MSGFILE    = MsgFile.FILEPART
  193.  
  194.     WRITEBREVENT bbsname '"'Tags.i.SYSTEM'"' event 0 stem EventData
  195.  
  196.     if RC > 0 then call ExitMsg(BBSREAD.LASTERROR)
  197. return
  198.  
  199.  
  200. ReadConfig:
  201.     /* Tags.0 will contains the tag numbers, Tags.X will contains the tag name
  202.     ** Tags.X.y will be defined as follow:
  203.     **     Tags.X.System     System name
  204.     **     Tags.X.Conf       Conference name
  205.     **     Tags.X.ToName     To user name
  206.     **     Tags.X.ToAddr     To user address
  207.     **     Tags.X.Subject    Message subject
  208.     **     Tags.X.BdyFile    File to be used as message body
  209.     **     Tags.X.SigFile    File to be used as message sign
  210.     **     Tags.X.HdrFile    File to be used as message header
  211.     **     Tags.X.FtrFile    File to be used as message footer
  212.     **     Tags.X.ExtPgm     External pgm that will create the BdyFile
  213.     **     Tags.X.ExtPgmType External pgm type (0 = ARexx / 1 = dos)
  214.     **     Tags.X.Days       Post message every Y days
  215.     **     Tags.X.Active     Tags.X active or not? (1 = True / 0 = False)
  216.     **     Tags.X.Prog_PVT   Progressive number, private field updated by the pgm!
  217.     **     Tags.X.Date_PVT   Last posted date, private field updated by the pgm!
  218.     */
  219.     drop Tags.
  220.     Tags.0  = 0
  221.     TagsNum = 0
  222.  
  223.     CfgOpen = open(cfgfile,ConfigFile,'r')
  224.  
  225.     if ~(CfgOpen) then call ExitMsg('Reading: failed to open' ConfigFile)
  226.  
  227.     do until eof(cfgfile)
  228.         nextline = readln(cfgfile)
  229.  
  230.         if compress(nextline) = "" then iterate
  231.  
  232.         parse var nextline CfgName CfgVal
  233.         CfgName = upper(CfgName)
  234.         CfgVal  = strip(compress(CfgVal,'"'))
  235.  
  236.         if CfgName = 'TAG' then do
  237.            TagsNum = TagsNum + 1
  238.            Tags.TagsNum = CfgVal
  239.          end
  240.          else do
  241.  
  242.            if TagsNum = 0 then call ExitMsg('No Tag names found!')
  243.  
  244.            if find(upper(TagOptions), CfgName) > 0 then
  245.               Tags.TagsNum.CfgName = CfgVal
  246.              else
  247.               call ExitMsg("Option '" || CfgName || "' (with value '" || CfgVal || "') in tag '" || Tags.TagsNum || "' not allowed!")
  248.         end
  249.     end
  250.  
  251.     if TagsNum = 0 then call ExitMsg('No Tag names found!')
  252.  
  253.     Tags.0 = TagsNum
  254.  
  255.     if (CfgOpen) then dummy = close(cfgfile)
  256. return
  257.  
  258.  
  259. WriteConfig:
  260.     CfgOpen = open(cfgfile,ConfigFile,'W')
  261.  
  262.     if ~(CfgOpen) then call ExitMsg('Saving: failed to open' ConfigFile)
  263.  
  264.     NL = trunc( length(TagOptions) / NumOpts)
  265.     NL = NL + trunc( (length(TagOptions) - NL) / NumOpts)
  266.  
  267.     do x = 1 to Tags.0
  268.        call writeln(cfgfile,'TAG  ' Tags.x)
  269.  
  270.        do y = 1 to NumOpts
  271.           OptL = word(TagOptions, y)
  272.           Opt  = upper(OptL)
  273.  
  274.           if Tags.x.Opt ~= "" & Tags.x.Opt ~= 0 | Opt = 'ACTIVE' then do
  275.  
  276.              if pos(" ",Tags.x.Opt) > 0 then
  277.                 call writeln(cfgfile,'   ' || pad(OptL,NL) || '"' || Tags.x.Opt || '"')
  278.                else
  279.                 call writeln(cfgfile,'   ' || pad(OptL,NL) || Tags.x.Opt)
  280.           end
  281.        end
  282.  
  283.        call writeln(cfgfile,'')
  284.     end
  285.  
  286.     if (CfgOpen) then dummy = close(cfgfile)
  287.  
  288.     address command 'copy >NIL: clone' ConfigFile 'EnvArc:Thor'
  289. return
  290.  
  291.  
  292.  
  293. /*
  294. ** Returns the current Day number
  295. */
  296. Dom:
  297. return Word( Date(N), 1)
  298.  
  299.  
  300.  
  301. /* pad a string with blank to the left
  302. **  parm1       string to be padded with blank
  303. **  parm2       new lenght
  304. */
  305. Pad:
  306. return left( arg(1) || copies(' ', arg(2) ), arg(2) )
  307.  
  308.  
  309.  
  310. /* Exit with a message */
  311. ExitMsg:
  312.     parse arg msgstr
  313.     address command
  314.  
  315.     if symbol('MsgFile.NAME') = 'VAR' then do
  316.         call close(out)
  317.         'delete >NIL:' MsgFile.NAME
  318.     end
  319.  
  320.     'RequestChoice >NIL: "GMAutoWrite.thor" "'msgstr'" "OK :-("'
  321. exit
  322.  
  323.